package ui.activity;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AddPassengerBean;
import com.fy.fyzf.bean.AreaBean;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.PassengerDetailBean;
import com.fy.fyzf.bean.PassengerListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.dialog.LoopViewPopwindow;
import com.fy.fyzf.presenter.PassengerPresnter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.GetJsonDataUtil;
import com.fy.fyzf.view.PassengerCallBack;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

//编辑需求
public class EditDemandActivity extends BaseActivity<PassengerPresnter> implements PassengerCallBack {

    @BindView(R.id.tv_quality)
    TextView tvQuality;
    @BindView(R.id.et_client_name)
    EditText etClientName;
    @BindView(R.id.tv_owner)
    TextView tvOwner;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_wechat)
    EditText etWechat;
    @BindView(R.id.et_QQ)
    TextView etQQ;
    @BindView(R.id.et_deal_type)
    TextView etDealType;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_distirct)
    TextView tvDistirct;
    @BindView(R.id.tv_site)
    TextView tvSite;
    @BindView(R.id.et_type)
    TextView etType;
    @BindView(R.id.et_source)
    TextView etSource;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.et_area)
    EditText etArea;
    @BindView(R.id.budget)
    TextView budget;
    @BindView(R.id.et_budget)
    EditText etBudget;
    @BindView(R.id.et_location_detail)
    EditText etLocationDetail;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_zhuangtai)
    TextView tvZhuangTai;
    @BindView(R.id.tv_quyu)
    TextView tvQuYu;
    @BindView(R.id.tv_didian)
    TextView tvDidian;
    @BindView(R.id.tv_gradle)
    TextView tvGradle;

    private String jsonFileName = "areaData.json";

    private PassengerDetailBean passengerDetailBean;
    private AddPassengerBean addPassengerBean = new AddPassengerBean();
    private List<AreaBean> areaBeanList;
    private Integer districtId;
    private int type;

    @Override
    protected PassengerPresnter createPresenter() {
        return new PassengerPresnter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_edit_demand;
    }

    @Override
    public void initView() {
        super.initView();

        tvQuality.setText(showColorText("*姓名"));
        tvOwner.setText(showColorText("*手机"));
        tvType.setText(showColorText("*交易类型"));
        tvQuYu.setText(showColorText("*区域"));
        tvDidian.setText(showColorText("*地点"));
        tvZhuangTai.setText(showColorText("*状态"));
        tvArea.setText(showColorText("*面积"));
        budget.setText(showColorText("*预算"));

        addPassengerBean.setCustomerStatus(0);
        addPassengerBean.setActualStatus(1);
        addPassengerBean.setCustomerType(2);

        String JsonData = new GetJsonDataUtil().getJson(this, jsonFileName);
        areaBeanList = new Gson().fromJson(JsonData, new TypeToken<List<AreaBean>>() {
        }.getType());

    }

    @Override
    public void initData() {
        super.initData();
        passengerDetailBean = (PassengerDetailBean) getIntent().getSerializableExtra("data");
        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {
            setTitleName("添加客源");
        } else {
            setTitleName("编辑求租需求");
            etClientName.setText(passengerDetailBean.getCustomerName());
            etPhone.setText(passengerDetailBean.getCustomerMobile());
            //设置交易类型bean
            addPassengerBean.setCustomerStatus(passengerDetailBean.getCustomerStatus());
            switch (passengerDetailBean.getCustomerStatus()) {
                case 0:
                    etDealType.setText("求租");
                    break;
                case 1:
                    etDealType.setText("求购");
                    break;
                case 2:
                    etDealType.setText("租购");
                    break;
            }
            etWechat.setText(passengerDetailBean.getWeChat());
            etQQ.setText(passengerDetailBean.getQq());

            tvDistirct.setText(passengerDetailBean.getParentAreaName());
            addPassengerBean.setDistrictId(passengerDetailBean.getParentAreaId());
            addPassengerBean.setCustomerId(passengerDetailBean.getCustomerId());
            //设置交易状态bean
            addPassengerBean.setActualStatus(passengerDetailBean.getActualStatus());

            tvGradle.setText(passengerDetailBean.getGrade());
            etLocationDetail.setText(passengerDetailBean.getPosition());
            switch (passengerDetailBean.getActualStatus()) {
                case 1:
                    tvStatus.setText("有效");
                    break;
                case 2:
                    tvStatus.setText("已租");
                    break;
                case 3:
                    tvStatus.setText("暂缓");
                    break;
                case 4:
                    tvStatus.setText("非有效");
                    break;
                case 5:
                    tvStatus.setText("已购");
                    break;
            }
            //设置房源类型
            addPassengerBean.setCustomerType(passengerDetailBean.getCustomerType());
            switch (passengerDetailBean.getCustomerType()) {
                case 1:
                    etType.setText("公寓");
                    break;
                case 2:
                    etType.setText("写字楼");
                    break;
                case 3:
                    etType.setText("商铺");
                    break;
                case 4:
                    etType.setText("住宅");
                    break;
            }

            //设置客户来源bean
            addPassengerBean.setCustomerSource(passengerDetailBean.getCustomerSource());
            if (passengerDetailBean.getCustomerSource() != null) {
            switch (passengerDetailBean.getCustomerSource()) {
                case 1:
                    etSource.setText("公司客户");
                    break;
                case 2:
                    etSource.setText("端口");
                    break;
                case 3:
                    etSource.setText("合作");
                    break;
                case 4:
                    etSource.setText("老客户");
                    break;
                case 5:
                    etSource.setText("物业介绍");
                    break;
            }
            }
            //面积
//            getCustomerArea
            districtId = passengerDetailBean.getParentAreaId();
            etArea.setText(passengerDetailBean.getCustomerArea()+"");
            addPassengerBean.setCustomerArea(passengerDetailBean.getCustomerArea());
            addPassengerBean.setCustomerRange(passengerDetailBean.getCustomerRange());
            addPassengerBean.setPosition(passengerDetailBean.getPosition());
            etBudget.setText(passengerDetailBean.getCustomerRange());
        }
    }
    List<String> titleList;
    List<Integer> idS ;
    @OnClick({R.id.tv_gradle,R.id.tv_distirct,R.id.tv_status,R.id.et_deal_type,R.id.tv_count, R.id.tv_ok,R.id.tv_quyu,R.id.tv_site, R.id.et_type,R.id.et_source})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et_deal_type:
                titleList = new ArrayList<>();
                idS = new ArrayList<>();

                titleList.add("求租");
                titleList.add("求购");
                titleList.add("租购");

                idS.add(0);
                idS.add(1);
                idS.add(2);
                getValue(1, titleList, idS, etDealType);
                break;
            case R.id.tv_status:
                titleList = new ArrayList<>();
                idS = new ArrayList<>();

                titleList.add("有效");
                titleList.add("已租");
                titleList.add("暂缓");
                titleList.add("非有效");
                titleList.add("已购");

                idS.add(1);
                idS.add(2);
                idS.add(3);
                idS.add(4);
                idS.add(5);
                getValue(2, titleList, idS, tvStatus);
                break;
            case R.id.tv_distirct:
                titleList = new ArrayList<>();
                idS = new ArrayList<>();

                for (int i = 0; i < areaBeanList.size(); i++) {
                    titleList.add(areaBeanList.get(i).getName());
                    idS.add(areaBeanList.get(i).getId());
                }

                getValue(3, titleList, idS, tvDistirct);
                break;
            case R.id.tv_site:
                if (districtId == null) {
                    AppUtils.showToast("请先选择区域");
                    return;
                }
                mPresenter.getSiteList(districtId);
                break;
            case R.id.et_type:
                titleList = new ArrayList<>();
                idS = new ArrayList<>();

                titleList.add("公寓");
                titleList.add("写字楼");
                titleList.add("商铺");
                titleList.add("住宅");

                idS.add(1);
                idS.add(2);
                idS.add(3);
                idS.add(4);

                getValue(5, titleList, idS, etType);
                break;
            case R.id.et_source:
                titleList = new ArrayList<>();
                idS = new ArrayList<>();

                titleList.add("公司客户");
                titleList.add("端口");
                titleList.add("合作");
                titleList.add("老客户");
                titleList.add("物业合作");

                idS.add(1);
                idS.add(2);
                idS.add(3);
                idS.add(4);
                idS.add(5);

                getValue(6, titleList, idS, etSource);
                break;
            case R.id.tv_gradle:
                titleList = new ArrayList<>();
                idS = new ArrayList<>();

                titleList.add("A");
                titleList.add("B");
                titleList.add("C");

                idS.add(1);
                idS.add(2);
                idS.add(3);
                getValue(7, titleList, idS, tvGradle);
                break;
            case R.id.tv_count:

                break;
            case R.id.tv_ok:
                if (etPhone.getText().toString().trim().length() != 11 || !etPhone.getText().toString().startsWith("1")) {
                    AppUtils.showToast("手机号码不正确");
                    return;
                }
                if (TextUtils.isEmpty(etClientName.getText().toString().trim())) {
                    AppUtils.showToast("姓名不能为空");
                    return;
                }

                if (TextUtils.isEmpty(etDealType.getText().toString().trim())) {
                    AppUtils.showToast("请选择交易类型");
                    return;
                }
                if (TextUtils.isEmpty(tvStatus.getText().toString().trim())) {
                    AppUtils.showToast("请选择状态");
                    return;
                }
                if (TextUtils.isEmpty(tvDistirct.getText().toString().trim())) {
                    AppUtils.showToast("请选区域");
                    return;
                }
                if (TextUtils.isEmpty(tvSite.getText().toString().trim())) {
                    AppUtils.showToast("请选择地点");
                    return;
                }
                if (TextUtils.isEmpty(etArea.getText().toString().trim())) {
                    AppUtils.showToast("请输入面积");
                    return;
                }
                if (TextUtils.isEmpty(etBudget.getText().toString().trim())) {
                    AppUtils.showToast("请输入预算");
                    return;
                }
                addPassengerBean.setCustomerName(etClientName.getText().toString());
                addPassengerBean.setCustomerMobile(etPhone.getText().toString().trim());
                addPassengerBean.setWeChat(etWechat.getText().toString());
                addPassengerBean.setQq(etQQ.getText().toString());
                addPassengerBean.setPosition(etLocationDetail.getText().toString().trim());
                addPassengerBean.setCustomerRange(etBudget.getText().toString());
                addPassengerBean.setCustomerArea(etArea.getText().toString());

//                addPassengerBean.set
                Log.e("添加客源数据", new Gson().toJson(addPassengerBean));
                //交易类型
                if (type == 1) {
                    mPresenter.addPassengerSource(addPassengerBean);
                } else {
                    mPresenter.editPassenger(addPassengerBean);
                }
                break;
            default:
        }
    }

    /**
     *
     * @param type 1 交易类型 2 状态 3 区域 4 地点 5 类型 6 来源
     * @param titles
     * @param idS
     * @param textView
     * @return
     */
    public void getValue( int type, List<String> titles, List<Integer> idS, TextView textView){
        LoopViewPopwindow loopViewPopwindow = new LoopViewPopwindow(this);
        loopViewPopwindow.setIds(idS).setTitleData(titles).setTitle(" ").showPop(etArea);

        loopViewPopwindow.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loopViewPopwindow.dismiss();

                textView.setText(loopViewPopwindow.getSelectName());
                if (type == 1) {
                    addPassengerBean.setCustomerName(etClientName.getText().toString());
                } else if(type == 2){
                    addPassengerBean.setActualStatus(idS.get(loopViewPopwindow.position));
                }else if(type == 3){
                    //然后再调用下一级
                    districtId = idS.get(loopViewPopwindow.position);
                }else if(type == 4){
                    addPassengerBean.setDistrictId(idS.get(loopViewPopwindow.position));
                }else if(type == 5){
                    addPassengerBean.setCustomerType(idS.get(loopViewPopwindow.position));
                } else if (type == 6) {
                    addPassengerBean.setCustomerSource(idS.get(loopViewPopwindow.position));
                } else {
                    //等级
                    addPassengerBean.setGrade(titles.get(loopViewPopwindow.position));
                }
               idS.get(loopViewPopwindow.position);
            }
        });
    }

    private SpannableStringBuilder showColorText(String s) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(s);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#E12002")), 0, 1, spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 1, s.length(), spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    }

    @Override
    public void getPassengerListSuccess(PassengerListBean passengerListBean) {

    }

    @Override
    public void getPassengerDetailSuccess(PassengerDetailBean passengerDetailBean) {

    }

    @Override
    public void getCollectHousingSuccess(HousesListBean housesListBean) {

    }

    @Override
    public void getMatchHousingSuccess(HousesListBean housesListBean) {

    }

    @Override
    public void getRecommendHousingSuccess(HousesListBean housesListBean) {

    }

    @Override
    public void collectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void removecollectHousingSuccess(BaseData baseData, int i) {

    }

    @Override
    public void AddPassengerSourceSuccess(BaseData baseData) {
        finish();
        EventBus.getDefault().post("editPassengerSuccess");
    }

    @Override
    public void getSiteListSuccess(List<AreaListBean> areaListBeans) {
        titleList = new ArrayList<>();
        idS = new ArrayList<>();
        for (int i = 0; i < areaListBeans.size(); i++) {
            titleList.add(areaListBeans.get(i).getAreaName());
            idS.add(areaListBeans.get(i).getAreaId());
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                getValue(4, titleList, idS, tvSite);
            }
        });
    }

    @Override
    public void editPassengerSuccess(BaseData baseData) {
        finish();
        AppUtils.showToast("编辑成功");
        EventBus.getDefault().post("editPassengerSuccess");
    }
}